<?php
/**
 * Created by PhpStorm.
 * User: long
 * Date: 2016/9/19
 * Time: 15:46
 */

namespace Consumer\Controller;


use Common\Controller\AdminbaseController;

use Common\Model\MemberModel;
use Common\Model\RechargeModel;
use Common\Model\SystemMsgModel;
use Common\Model\UsersModel;
use Common\Model\WalletBillModel;
use Common\Model\WalletModel;


class MemberController extends AdminbaseController
{
    private $member_model;
    private $users_model;
    private $system_msg_model;

    public function __construct()
    {
        parent::__construct();
        $this->member_model = new MemberModel();
        $this->users_model = new UsersModel();
        $this->system_msg_model = new SystemMsgModel();
    }

    /**
     * 用户列表
     */
    public function lists()
    {
        $this->_lists();
        $this->display();
    }



    private function _lists()
    {
        if(IS_POST) {
            $username = I('post.username');
            $nickname = I('post.nickname');
            $start_time = I('post.start_time');
            $end_time = I('post.end_time');
            $login_type = I('post.login_type');
            if($username) {
                $where['a.username'] = array('like', '%'.$username.'%');
            }
            if($nickname) {
                $where['a.nickname'] = array('like', '%'.$nickname.'%');
            }

            if($start_time && $end_time) {
                $where['a.create_time'] = ['between', [$start_time.' 00:00:00', $end_time.' 23:59:59']];
            }
        } else {
            $username = I('get.username');
            $nickname = I('get.nickname');
            $start_time = I('get.start_time');
            $end_time = I('get.end_time');
            $login_type = I('get.login_type');
            if($username) {
                $where['a.username'] = array('like', '%'.$username.'%');
            }
            if($username) {
                $where['a.username'] = array('like', '%'.$username.'%');
            }
            if($nickname) {
                $where['a.nickname'] = array('like', '%'.$nickname.'%');
            }

            if($start_time && $end_time) {
                $where['a.create_time'] = ['between', [$start_time.' 00:00:00', $end_time.' 23:59:59']];
            }
        }

        $count = $this->member_model
            ->alias('a')
            ->where($where)
            ->count();
        $page = $this->page($count, C("PAGE_NUMBER"));
        $result = $this->member_model
            ->alias('a')
//            ->join('left join dr_city as b on a.city = b.id')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->where($where)
            ->field('a.*')
            ->order('a.id desc')
            ->select();


        $categorys = '';
        foreach ($result as $k => $v) {

//            $result[$k]['headimg'] = '<a target="_blank" href="' . $v['headimg'] . '">查看</a>';

            $result[$k]['str_manage'] = '<a href="' . U('Consumer/Member/detail', ['id' => $v['id']]) . '">查看信息</a>  ';//| <a data-toggle="modal" data-target="#myModal_2"  class="add_ext_2"  onclick="" cool="' . $v['username'] . '" name="' . $v['id'] . '">推送消息</a><a data-toggle="modal" data-target="#myModal_remarks"  class="add_ext"  onclick="" cool="' . $v['username'] . '" name="' . $v['id'] . '">备注</a> |

            $categorys .= '<tr>
            <td>' . ($k + 1) . '</td>
            <td>' . $v['username'] . '</td>
            <td>' . $v['phone'] . '</td>
            <td>' . $v['nickname'] . '</td>
            <td>' . $v['create_time'] . '</td>
            <td>' . $v['last_login'] . '</td>
            <td style="white-space:nowrap;">' . $result[$k]['str_manage'] . '</td>
        </tr>';

        }

        $this->assign('formget', I(''));
        $this->assign('login_type', $login_type);
        $this->assign('categorys', $categorys);
        $this->assign("Page", $page->show());
        $this->assign('count', $count);
    }




    /**
     * 推送消息
     */
    public function PushMsg() {
        $mid = I('post.mid');
        $content = I('post.content');

        pushMess($content, [$mid]);
        if($this->system_msg_model->addOne($mid, $content) == false) {
            $this->error();
        } else {
            $this->success();
        }
    }


    public function detail()
    {
        $mid = I('get.id');
        $info = $this->member_model
            ->alias('a')
//            ->join('LEFT JOIN dr_city as b ON a.city = b.id')
            ->where(['a.id' => $mid])
            ->field('a.*')
            ->find();

//        var_dump($info);
        $info['header'] = $info['header'] ? setUrl($info['header']) : '';
        $this->assign('info', $info);
        $this->display();
    }


}